import { Controller, Get, Post, Body, Patch, Param, Delete, Request, Req, Res} from '@nestjs/common';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';

import * as svgCaptcha from 'svg-captcha';

@Controller({
    path: 'user',
    version: '1'
})
export class UserController {
    constructor(private readonly userService: UserService) { }

    @Post()
    create(@Body() createUserDto: CreateUserDto) {
        return this.userService.create(createUserDto);
    }

    // @Get()
    // findAll() {
    //     return this.userService.findAll();
    // }
    
    // 获取验证码
    @Get('code')
    createCaptcha(@Req() req, @Res() res) {
        const captcha = svgCaptcha.create({
            size: 4,
            fontSize: 50,
            width: 100,
            height: 34,
            background: '#cc9966'
        });
        console.log(captcha.text)
        req.session.code = captcha.text;
        res.type('image/svg+xml');
        res.send(captcha.data);
    }
    
    // 创建用户
    @Post('create')
    createUser(@Req() req, @Body() body) {
        console.log(req.session.code, body);
        if(req.session.code.toLocaleLowerCase() === body?.code?.toLocaleLowerCase()){
            return {
                message: '验证码正确'
            }
        }else{
            return {
                message: '验证码错误'
            }
        }
    }
    
    @Get()
    find(@Request() req){
        console.log(req.query);
        return {code: 200}
    }

    @Get(':id')
    findOne(@Param('id') id: string) {
        return this.userService.findOne(+id);
    }

    @Patch(':id')
    update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
        return this.userService.update(+id, updateUserDto);
    }

    @Delete(':id')
    remove(@Param('id') id: string) {
        return this.userService.remove(+id);
    }
}
